/****** Object:  Stored Procedure dbo.CGO_GameEnrollmentSave    Script Date: Sunday, March 06, 2011 ******/
IF EXISTS ( SELECT  *
            FROM    dbo.sysobjects
            WHERE   id = OBJECT_ID(N'dbo.CGO_GameEnrollmentSave')
                    AND OBJECTPROPERTY(id, N'IsProcedure') = 1 ) 
  DROP PROCEDURE dbo.CGO_GameEnrollmentSave
GO

/* -------------------------------------------------------------------------------------
/   GameEnrollmentAdd
/  ------------------------------------------------------------------------------------- */
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE dbo.CGO_GameEnrollmentSave
  @EnrollmentID INT ,
  @PortalID INT ,
  @EventID INT ,
  @GameID INT ,
  @UserID INT ,
  @EnrollStatusID INT ,
  @EnrollFee MONEY ,
  @StartTime DATETIME ,
  @EndTime DATETIME ,
  @ConnectCount INT ,
  @Score DECIMAL(18, 0)
AS 
  DECLARE @RetID INT
  SET @RetID = 0
  
  DECLARE @StatusBefore VARCHAR(100) 
  DECLARE @StatusAfter VARCHAR(100)
  DECLARE @Action VARCHAR(100)
  
  IF @EnrollmentID IS NOT NULL
    AND @EnrollmentID > 0
    AND EXISTS ( SELECT 1
                 FROM   dbo.CGO_GameEnrollment
                 WHERE  EnrollmentID = @EnrollmentID
                        AND PortalID = @PortalID ) 
    BEGIN
      SELECT  @StatusBefore = S.StatusCode
      FROM    dbo.CGO_GameEnrollment M
              INNER JOIN dbo.CGO_GameEnrollmentStatus S ON M.EnrollStatusID = S.StatusID
      WHERE   PortalID = @PortalID
              AND EnrollmentID = @EnrollmentID
              
      SELECT  @StatusAfter = StatusCode
      FROM    dbo.CGO_GameEnrollmentStatus
      WHERE   StatusID = @EnrollStatusID
      
      UPDATE  dbo.CGO_GameEnrollment
      SET     EnrollStatusID = @EnrollStatusID ,
              EndTime = @EndTime ,
              ConnectCount = @ConnectCount ,
              Score = @Score ,
              LastUpdateDate = GETDATE()
      WHERE   PortalID = @PortalID
              AND EnrollmentID = @EnrollmentID
      
      SET @RetID = @EnrollmentID
    END
  ELSE 
    BEGIN
      SET @StatusBefore = NULL
      
      SELECT  @StatusAfter = StatusCode
      FROM    dbo.CGO_GameEnrollmentStatus
      WHERE   StatusID = @EnrollStatusID
      
      INSERT  INTO dbo.CGO_GameEnrollment
              ( PortalID ,
                EventID ,
                GameID ,
                UserID ,
                EnrollStatusID ,
                EnrollFee ,
                StartTime ,
                EndTime ,
                ConnectCount ,
                Score ,                
                CreatedDate ,                
                LastUpdateDate
              )
      VALUES  ( @PortalID ,
                @EventID ,
                @GameID ,
                @UserID ,
                @EnrollStatusID ,
                @EnrollFee ,
                @StartTime ,
                NULL ,
                0 ,
                0 ,
                GETDATE() ,
                GETDATE()
              )
      SET @RetID = SCOPE_IDENTITY()
    END
  
  IF @StatusBefore IS NULL AND @StatusAfter IS NOT NULL
  BEGIN
  	SET @Action = 'ENROLL'
  END
  
  IF @StatusBefore = 'ENROLLED' AND @StatusAfter ='PAID'
  BEGIN
  	SET @Action = 'PAY'
  END
  
  IF @StatusBefore = 'PAID' AND @StatusAfter ='PLAYING'
  BEGIN
  	SET @Action = 'START'
  END
  
  IF @StatusBefore = 'PLAYING' AND @StatusAfter ='PLAYING'
  BEGIN
  	SET @Action = 'RECONNECT'
  END
  
  IF @StatusBefore = 'PLAYING' AND @StatusAfter ='COMPLETED'
  BEGIN
  	SET @Action = 'FINISH'
  END
  
  INSERT INTO dbo.CGO_GameEnrollmentLog
          ( EnrollmentID ,
            Action ,
            StatusBefore ,
            StatusAfter ,
            LogTime
          )
  VALUES  ( @RetID , -- EnrollmentID - int
            @Action , -- Action - nvarchar(100)
            @StatusBefore , -- StatusBefore - nvarchar(100)
            @StatusAfter , -- StatusAfter - nvarchar(100)
            GETDATE()  -- LogTime - datetime
          )
  
  SELECT  @RetID
  
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO